package com.mountainreacher.socketconnection;

import java.util.Collection;

import communication.MessageDistpacher;

import protocol.*;
import util.NetworkUtility;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ConnectionActivity extends Activity implements OnClickListener, OnItemClickListener, OnConnectionUpdatesListener {

	// Visual elements
	private ArrayAdapter<String> listAdapter;
	private ListView listView;
	private EditText yourName;
	private Button changeName;

	// Communication elements
	private ConnectionDiscover connectionDiscover;
	//	private PairedConnection pairedConnection;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connection);
		init();
		connectionDiscover = new ConnectionDiscover("android", NetworkUtility.getIpAddr((WifiManager) getSystemService(WIFI_SERVICE)), this, MessageDistpacher.INSTANCE);
	}

	private void init() {
		changeName = (Button) findViewById(R.id.changeName);
		changeName.setOnClickListener(this);
		yourName = (EditText) findViewById(R.id.yourName);
		yourName.setText("android");
		listView = (ListView) findViewById(R.id.usersList);
		listView.setOnItemClickListener(this);
		listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
		listView.setAdapter(listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.connection, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if(yourName.getText().length() < 3)
			Toast.makeText(this, "The name is too short", 0).show();
		else {
			try {connectionDiscover.changeUsername(yourName.getText().toString());} catch (Exception e) {}
		}

	}

	@Override
	public void onUsersListUpdated(final Collection<User> users) {

		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				listAdapter.clear();
				for (User user : users)
					listAdapter.add(user.toString());
				listAdapter.notifyDataSetChanged();
			}
		});

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int clicPos, long arg3) {
		Intent intent = new Intent(this, ChatActivity.class);
		startActivity(intent);
		connectionDiscover.connectTo(clicPos);
	}

	@Override
	public void onNewConnection(final User user) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				
				Toast.makeText(getApplicationContext(), user.toString(), 0).show();
				
//				AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
//				alert.setTitle("Do you want to connect to");
//				alert.setMessage(user.toString()+"?");
//				alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
//					public void onClick(DialogInterface dialog, int whichButton) {
//						// Canceled.
//					}
//				});
//				alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//					public void onClick(DialogInterface dialog, int whichButton) {
//						Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
//						startActivity(intent);
//					}
//				});
//
//				alert.show();
			}
		});
		
		Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
		startActivity(intent);
		

	}

}
